package com.fate.securecheckplatform.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.fate.securecheckplatform.R;
import com.fate.securecheckplatform.bean.TableForbiddenPerformance;
import com.fate.securecheckplatform.bean.TableForbiddenThing;
import com.fate.securecheckplatform.utils.Util;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Author:   RandBII
 * DATE:   2020/1/8
 * Description:
 */
public class RegisterAdapter extends RecyclerView.Adapter<RegisterAdapter.ViewHolder> implements View.OnClickListener {

    private Context context;
    private List<TableForbiddenPerformance> datas;
    private OnItemClickListener listener;

    public RegisterAdapter(Context context, List<TableForbiddenPerformance> datas) {
        this.context = context;
        this.datas = datas;
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View root = LayoutInflater.from(context).inflate(R.layout.item_register, parent, false);
        root.setOnClickListener(this);
        return new ViewHolder(root);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.itemView.setTag(position);
        TableForbiddenPerformance performance = datas.get(position);

        if (performance != null) {
            if (performance.getAndroidSelected()) {
                holder.itemView.setBackgroundColor(context.getResources().getColor(R.color.teal_200));
            } else {
                holder.itemView.setBackgroundColor(context.getResources().getColor(R.color.white));
            }
        }


        holder.mTvSerialNum.setText(String.valueOf(performance.getId()));
        holder.mTvCheckPerson.setText(Util.toFillView(performance.getJcry()));
        holder.mTvCheckJob.setText(Util.toFillView(performance.getJcgw()));
        holder.mTvResolvePersion.setText(Util.toFillView(performance.getCzry()));
        holder.mTvResovePolice.setText(Util.toFillView(performance.getCzmj()));
        holder.mTvlkName.setText(Util.toFillView(performance.getLkxm()));
        holder.mTvIDNumber.setText(Util.toFillView(performance.getSfzh()));
        holder.mTvPhone.setText(Util.toFillView(performance.getLxfs()));
        holder.mTvTrainNum.setText(Util.toFillView(performance.getCc()));
        if ("1".equals(performance.getSfla())) {
            holder.mTvDocumentStatus.setText("是");
        } else {
            holder.mTvDocumentStatus.setText("否");
        }

//        wpcz  物品状态（0-未入库 1-移入暂存库 2-移入危险库 3-民警处置 4-销毁 5-暂存物品出库 6-危险物品出库）

        List<TableForbiddenThing> things = performance.getThins();
        if (things != null && things.size() > 0) {
            holder.mTvObjectName.setText(Util.toFillView(things.get(0).getWjpmc()));
            holder.mTvCountNum.setText(Util.toFillView(things.get(0).getWjpsl()));
            switch (things.get(0).getWpcz()) {
                case "移入暂存库":
                    holder.mTvObjectStatus.setText("移入暂存库");
                    break;
                case "移入危险库":
                    holder.mTvObjectStatus.setText("移入危险库");
                    break;
                case "民警处置":
                    holder.mTvObjectStatus.setText("民警处置");
                    break;
            }
        }

    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    @Override
    public void onClick(View v) {
        if (listener != null) {
            listener.onItemClick(v, (Integer) v.getTag());
        }
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_serial_num)
        TextView mTvSerialNum;
        @BindView(R.id.tv_check_person)
        TextView mTvCheckPerson;
        @BindView(R.id.tv_check_job)
        TextView mTvCheckJob;
        @BindView(R.id.tv_resove_person)
        TextView mTvResolvePersion;
        @BindView(R.id.tv_resove_police)
        TextView mTvResovePolice;
        @BindView(R.id.tv_object_name)
        TextView mTvObjectName;
        @BindView(R.id.tv_count_num)
        TextView mTvCountNum;
        @BindView(R.id.tv_traveller_name)
        TextView mTvlkName;
        @BindView(R.id.tv_id_card_number)
        TextView mTvIDNumber;
        @BindView(R.id.tv_phone)
        TextView mTvPhone;
        @BindView(R.id.tv_train_num)
        TextView mTvTrainNum;
        @BindView(R.id.tv_object_status)
        TextView mTvObjectStatus;
        @BindView(R.id.tv_document_status)
        TextView mTvDocumentStatus;


        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

    }
}
